<?php
/**
 * Mechanism
 *
 * LICENSE
 *
 * Copyright (c) 2011, Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following disclaimer
 *   in the documentation and/or other materials provided with the
 *   distribution.
 * * Neither the name of the  nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @package Mechanism
 * @license http://www.opensource.org/licenses/bsd-license
 * @author Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * @copyright Copyright (c) 2011 Sergey A. Ivonchik
 */

namespace Mechanism\Router\Route {
	/**
	 * HostTest
	 *
	 * Host route test case.
	 *
 	 * @package Mechanism\Router\Route
 	 * @author Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
	 */
	class HostTest extends \PHPUnit_Framework_TestCase {
		/**
		 * Route for tests
		 *
		 * @var Mechanism\Router\Route\Host
		 */
		protected $route = null;

		/**
		 * Setup method
		 */
		public function setUp( ) {
			$this->route = new Host(
				'[module]{s}{h:example.com}',
				array( 'module' => 'main' )
			);
		}

		/**
		 * Test for match( $path ) method
		 *
		 * @test
		 */
		public function match( ) {
			$testHostA = 'http://www.example.com/';
			$testHostAResult = array( 'module' => 'main' );

			$testHostB = 'http://music.example.com/';
			$testHostBResult = array( 'module' => 'music' );

			$testHostC = 'www.music.example.com';
			$testHostCResult = array( 'module' => 'music' );

			$testHostD = 'http://www.sub.music.example.com/';

			/* Test valid A */
			$this->assertTrue(
				$this->route->match( $testHostA )
			);
			$this->assertEquals(
				$testHostAResult,
				$this->route->get( )
			);

			/* Test valid B */
			$this->assertTrue(
				$this->route->match( $testHostB )
			);
			$this->assertEquals(
				$testHostBResult,
				$this->route->get( )
			);

			/* Test valid C */
			$this->assertTrue(
				$this->route->match( $testHostC )
			);
			$this->assertEquals(
				$testHostCResult,
				$this->route->get( )
			);
			
			/* Test invalid D */
			$this->assertFalse(
				$this->route->match( $testHostD )
			);

			
			/* Even domain route ;) */
			$domainRoute = new Host( '[module]{s}{h:example}{s}[domain]' );
			$domainRoute->setDefault( 'module', 'main' )
					    ->setRule( 'domain', '[a-z]{2,4}' );

			$this->assertTrue(
				$domainRoute->match( 'http://music.example.by/' )
			);
			$this->assertEquals(
				array(
					'module' => 'music',
					'domain' => 'by'
				),
				$domainRoute->get( )
			);

			/* Invalid */
			$this->assertFalse(
				$domainRoute->match( 'http://localhost/' )
			);
			$this->assertFalse(
				$domainRoute->match( 'http://www.example.of.com/' )
			);
					
			/* With required params */
			$withoutParamsRoute = new Host( '[username]{s}{h:example.com}' );
			$withoutParamsRoute->requireParams( );
			
			/* Cause we do not specity params */
			$this->assertFalse(
				$withoutParamsRoute->match( 'http://www.example.com/' )
			);			

			/* Cause no defaults and setted params */
			$this->assertFalse(
				$withoutParamsRoute->match( 'http://www.sergey.example.com/' )
			);		

			/* With setted Params */
			$withoutParamsRoute->set( 'controller', 'users' )
							   ->set( 'action', 'show' );
							   
			$this->assertTrue(
				$withoutParamsRoute->match( 'http://www.sergey.example.com/' )
			);	
		}
	}
}